package top.hudk.sql.generator;

import com.yueyue.common.utils.JsonUtils;
import com.yueyue.unicorn.finance.common.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import top.hudk.sql.domin.InsuranceRecordDataMate;
import top.hudk.sql.domin.ShardingResult;
import top.hudk.sql.strategy.WeekShardingStrategy;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 请输入描述信息
 * ShardingStrategy
 * @author HuDongkang
 * @date 2022/9/1 10:37
 */
@Slf4j
public class InsuranceSqlGenerator extends AbstractSqlGenerator<InsuranceRecordDataMate, WeekShardingStrategy, String> {

    private static final String SQL_TEMPLATE = "UPDATE `leopard_finance_insurance`.`finance_unicorn_insurance_%s` set state = 4 , cashier_receipt_time = '2022-10-09 00:00:00' where order_id = '%s';";

    public static void main(String[] args) {
        new InsuranceSqlGenerator().gen();
    }

    @Override
    protected ShardingResult doShardingStrategy(WeekShardingStrategy shardingStrategy, String shardingValue) {
        Date acceptOrderTime = str2Date(shardingValue);
        return shardingStrategy.sharding(acceptOrderTime);
    }

    @Override
    protected String format(ShardingResult shardingResult, InsuranceRecordDataMate dataMate){
        return String.format(SQL_TEMPLATE,
                shardingResult.getTableNo(),
                dataMate.getOrderId());
    }

    @Override
    protected WeekShardingStrategy getShardingStrategy() {
        return new WeekShardingStrategy();
    }

    @Override
    protected List<InsuranceRecordDataMate> getDataMateList(){
        return new ArrayList<>(JsonUtils.fromListValue(DATA_MATE_JSON_STR, InsuranceRecordDataMate.class));
    }

    private static final String DATA_MATE_JSON_STR =
            "[{\"acceptOrderTime\":\"2022-09-03 21:01:15\",\"orderId\":\"2209031743001089241\"},\n" +
                    "{\"acceptOrderTime\":\"2022-08-20 23:45:53\",\"orderId\":\"2208202998001154572\"},\n" +
                    "{\"acceptOrderTime\":\"2022-06-27 09:45:29\",\"orderId\":\"2206271359001019755\"}]";

    private Date str2Date(String timeStr){
        try {
            return DateUtils.str2Date(timeStr);
        }catch (Exception exception){
            throw new RuntimeException("日期解析失败");
        }
    }
}
